package com.isunland.managesystem.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.ArraySwipeAdapter;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.AttendanceDetail;
import java.util.List;

public class AttendanceSummaryAdapter extends ArraySwipeAdapter<AttendanceDetail>
    implements OnClickListener {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-12-15 16:38:59 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final SwipeLayout rootView;
    public final TextView tvNameAttendanceSummary;
    public final TextView tvDeptNameAttendanceSummary;
    public final TextView tvTimeAttendanceSummary;
    public final TextView tvSinInAttendanceSummary;
    public final TextView tvSinOutAttendanceSummary;
    public final ImageView mLLSwipe;
    public final ImageView mIvPhoto;
    public TextView tvPersonAttendanceSummary;
    public final TextView tvAttendGroupAttendanceSummary;
    public final TextView tvAttendStatusAttendanceSummary;

    private ViewHolder(SwipeLayout rootView, TextView tvNameAttendanceSummary,
        TextView tvDeptNameAttendanceSummary, TextView tvTimeAttendanceSummary,
        TextView tvSinInAttendanceSummary, TextView tvSinOutAttendanceSummary, ImageView mLLSwipe,
        TextView tvPersonAttendanceSummary, ImageView mIvPhoto,
        TextView tvAttendGroupAttendanceSummary, TextView tvAttendStatusAttendanceSummary) {
      this.rootView = rootView;
      this.tvNameAttendanceSummary = tvNameAttendanceSummary;
      this.tvDeptNameAttendanceSummary = tvDeptNameAttendanceSummary;
      this.tvTimeAttendanceSummary = tvTimeAttendanceSummary;
      this.tvSinInAttendanceSummary = tvSinInAttendanceSummary;
      this.tvSinOutAttendanceSummary = tvSinOutAttendanceSummary;
      this.mLLSwipe = mLLSwipe;
      this.tvPersonAttendanceSummary = tvPersonAttendanceSummary;
      this.mIvPhoto = mIvPhoto;
      this.tvAttendGroupAttendanceSummary = tvAttendGroupAttendanceSummary;
      this.tvAttendStatusAttendanceSummary = tvAttendStatusAttendanceSummary;
    }

    public static ViewHolder create(SwipeLayout rootView) {
      TextView tvNameAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_name_attendanceSummary);
      TextView tvDeptNameAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_deptName_attendanceSummary);
      TextView tvTimeAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_time_attendanceSummary);
      TextView tvSinInAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_sinIn_attendanceSummary);
      TextView tvSinOutAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_sinOut_attendanceSummary);
      ImageView mLLSwipe = (ImageView) rootView.findViewById(R.id.ll_swipe);
      TextView tvPersonAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_person_attendanceSummary);
      TextView tvAttendGroupAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_AttendGroup_attendanceSummary);
      TextView tvAttendStatusAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_attendStatus_attendanceSummary);
      ImageView mIvPhoto = (ImageView) rootView.findViewById(R.id.iv_photo);
      return new ViewHolder(rootView, tvNameAttendanceSummary, tvDeptNameAttendanceSummary,
          tvTimeAttendanceSummary, tvSinInAttendanceSummary, tvSinOutAttendanceSummary, mLLSwipe,
          tvPersonAttendanceSummary, mIvPhoto, tvAttendGroupAttendanceSummary,
          tvAttendStatusAttendanceSummary);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_attendance_summary, parent, false);
      vh = ViewHolder.create((SwipeLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }
    vh.mLLSwipe.setOnClickListener(this);
    vh.mLLSwipe.setTag(position);
    AttendanceDetail item = (AttendanceDetail) getItem(position);
    //人员姓名
    vh.tvPersonAttendanceSummary.setText(item.getName());
    //所在部门
    vh.tvDeptNameAttendanceSummary.setText(item.getDeptName());

    // 时间
    String date = DateFormat.format("MM月dd  EEEE", item.getDate()).toString();
    String time = DateFormat.format(" kk:mm", item.getCheckTime()).toString();
    vh.tvTimeAttendanceSummary.setText(date);
    //考勤状态
    vh.tvAttendStatusAttendanceSummary.setText(item.getAttendStatusName());
    // 打卡类型
    switch (item.getCheckType()) {
      case 0:
        vh.tvSinOutAttendanceSummary.setText(mContext.getString(R.string.sign_in) + time);
        //                vh.tvSinOutAttendanceSummary.setTextColor(getContext().getResources().getColor(R.color.blue_textColor));
        break;
      case 1:
        vh.tvSinOutAttendanceSummary.setText(mContext.getString(R.string.sign_out) + time);
        //                vh.tvSinOutAttendanceSummary.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case 2:

        vh.tvSinOutAttendanceSummary.setText(mContext.getString(R.string.sign_field_time) + time);
        //                vh.tvSinOutAttendanceSummary.setTextColor(getContext().getResources().getColor(R.color.yelloy_textColor));
        break;
    }

    //考勤分组,attendCardStatus为T打卡有效显示考勤分组
    if (!TextUtils.isEmpty(item.getAttendCardStatus()) && "T".equalsIgnoreCase(
        item.getAttendCardStatus())) {
      if (!TextUtils.isEmpty(item.getAttendGroupName())) {
        vh.tvAttendGroupAttendanceSummary.setVisibility(View.VISIBLE);
        vh.tvAttendGroupAttendanceSummary.setText(item.getAttendGroupName());
      } else {
        vh.tvAttendGroupAttendanceSummary.setVisibility(View.GONE);
      }
    } else {
      vh.tvAttendGroupAttendanceSummary.setVisibility(View.GONE);
    }

    // 备注
    String remarks = item.getRemark();// 内容

    if (!TextUtils.isEmpty(remarks)) {
      vh.tvNameAttendanceSummary.setVisibility(View.VISIBLE);
      String mote = mContext.getString(R.string.note);// 备注：

      SpannableString span = new SpannableString(mote + remarks);
      span.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.dark_note)),
          3, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

      vh.tvNameAttendanceSummary.setText(span);
    } else {
      vh.tvNameAttendanceSummary.setVisibility(View.GONE);
    }

    String filePath = item.getPhotoPath();// 打卡图片

    if (!TextUtils.isEmpty(filePath)) {
      vh.mIvPhoto.setVisibility(View.VISIBLE);
      vh.mIvPhoto.setTag(filePath);
      vh.mIvPhoto.setOnClickListener(this);
    } else {
      vh.mIvPhoto.setVisibility(View.INVISIBLE);
    }

    return vh.rootView;
  }

  private LayoutInflater mInflater;
  private Context mContext;
  private Callback mCallback;

  /**
   * 自定义接口，用于回调按钮点击事件到Activity
   */
  public interface Callback {
    public void click(View v);
  }

  // Constructors
  public AttendanceSummaryAdapter(Context context, List<AttendanceDetail> objects,
      Callback callback) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mContext = context;
    this.mCallback = callback;
  }

  public AttendanceSummaryAdapter(Context context, AttendanceDetail[] objects, Callback callback) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mContext = context;
    this.mCallback = callback;
  }

  @Override public boolean isEmpty() {
    // Auto-generated method stub
    return false;
  }

  @Override public int getSwipeLayoutResourceId(int arg0) {
    return R.id.swipe;
  }

  @Override public void onClick(View v) {
    mCallback.click(v);
  }
}
